<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item prop="searchTime" label="年月">
        <el-date-picker size="small"
                        v-model="queryParams.searchTime"
                        type="month"
                        value-format="yyyy-MM"
                        @change="handleQuery"
                        placeholder="选择年月" style="width:250px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="userEmployeeStatus" label="员工状态">
        <el-select clearable v-model="queryParams.userEmployeeStatus" placeholder="请选择员工状态" style="width:250px">
          <el-option
            v-for="item in employeeStatusOptions"
            :key="item.id"
            :label="item.label"
            :value="item.value"

          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="userPoliticalOutlook" label="政治面貌">
        <el-select clearable v-model="queryParams.userPoliticalOutlook" placeholder="请选择政治面貌" style="width:250px">
          <el-option
            v-for="item in mianmaoOptions"
            :key="item.id"
            :label="item.label"
            :value="item.value"

          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="jobnumber">
        <el-input
          prop="jobnumber"
          v-model="queryParams.jobnumber"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
          style="width:250px"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="nickName">
        <el-input
          prop="nickName"
          v-model="queryParams.nickName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
          style="width:250px"
        />
      </el-form-item>
      <el-form-item prop="userDeptId" label="部门">
        <treeselect v-model="queryParams.userDeptId" :options="deptOptions" :show-count="true" placeholder="请选择部门"
                    style="width:250px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="auditStatus">
        <el-select clearable v-model="queryParams.auditStatus" placeholder="请选择审核状态" style="width:250px">
          <el-option
            v-for="item in auditStatusOptions"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['compensationManagement:staffPayroll:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['compensationManagement:staffPayroll:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['compensationManagement:staffPayroll:remove']"
        >删除
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportArticleWages"
          v-hasPermi="['compensationManagement:staffPayroll:exportArticleWages']"
        >导出工资条
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportSalaryFiles"
          v-hasPermi="['compensationManagement:staffPayroll:exportSalaryFiles']"
        >导出工资档案
        </el-button>
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入历史工资
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe v-loading="loading" :data="staffPayrollList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column fixed label="工资编号" align="center" prop="staffpayrollId"/>
      <el-table-column fixed label="工号" align="center" prop="jobnumber"/>
      <el-table-column fixed label="姓名" align="center" prop="nickName"/>
      <el-table-column label="工资年月" align="center" prop="salaryyearmonth" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.salaryyearmonth, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生成时间" width="180" align="center" prop="staffpayrollCreateTime"/>
      <el-table-column label="岗位名称" align="center" prop="position"/>
      <el-table-column label="薪级名称" align="center" prop="salary" width="120"/>
      <el-table-column label="岗位工资" align="center" prop="positionSalary"/>
      <el-table-column label="薪级工资" align="center" prop="payWages"/>
      <el-table-column label="基础性绩效工资" align="center" prop="basicPerformancePay"/>
      <el-table-column label="奖励性绩效工资" align="center" prop="incentivizedPerformancePay"/>
      <el-table-column label="基础性绩效奖" align="center" prop="basicPerformanceAward"/>
      <el-table-column label="独生子女保健" align="center" prop="oneChildCare"/>
      <el-table-column label="住房补贴" align="center" prop="housingSubsidies"/>
      <el-table-column label="加班工资" align="center" prop="overtimePay"/>
      <el-table-column label="应发工资" align="center" prop="wagesPayable"/>
      <el-table-column label="实扣数" align="center" prop="actualDeduction"/>
      <el-table-column label="实发数" align="center" prop="actualSalary"/>
      <el-table-column label="是否确定" align="center" width="110" prop="auditStatus">
        <template slot-scope="{row}">
          <el-tag
            type="success"
            v-show="row.auditStatus==='Y'"
          >
            确定
          </el-tag>
          <el-tag
            type="info"
            v-show="row.auditStatus==='N'"
          > 未确定
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding" width="230">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="determinePayroll(scope.row)"
            v-hasPermi="['compensationManagement:staffPayroll:determiningStaffPayroll']"
          >确定工资表
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['compensationManagement:staffPayroll:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewModificationHistory(scope.row)"
            v-hasPermi="['compensationManagement:staffPayrollLog:list']"
          >历史记录
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['compensationManagement:staffPayroll:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工资表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="选择员工" prop="selectUser" v-show="form.staffpayrollId==null">
          <el-select v-model="form.selectUser" filterable remote style="width: 500px" reserve-keyword
                     placeholder="输入姓名进行选择" :remote-method="remoteMethod" @change="userChange()"
                     :loading="userObj.loading"
          >
            <el-option v-for="item in userObj.approverOptions" :key="item.value" :label="item.label"
                       :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="工号" prop="jobnumber">
          <el-input v-model="form.jobnumber" :disabled="form.staffpayrollId!=null" placeholder="请输入工号"/>
        </el-form-item>
        <el-form-item label="工资年月" prop="salaryyearmonth">
          <el-date-picker clearable
                          v-model="form.salaryyearmonth"
                          type="month"
                          value-format="yyyy-MM"
                          placeholder="请选择工资年月"
                          @blur="jobnumberChange()"
                          :disabled="form.staffpayrollId!=null"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="form.nickName" :disabled="form.staffpayrollId!=null" placeholder="输入工号和工资年月后自动查询显示"/>
        </el-form-item>
        <el-form-item label="岗位名称" prop="position">
          <el-input v-model="form.position" :disabled="form.staffpayrollId!=null" placeholder="输入工号和工资年月后自动查询显示"/>
        </el-form-item>
        <el-form-item label="薪级名称" prop="salary">
          <el-input v-model="form.salary" :disabled="form.staffpayrollId!=null" placeholder="输入工号和工资年月后自动查询显示"/>
        </el-form-item>
        <el-form-item label="员工类型" prop="employeeType">
          <el-input v-model="form.employeeType" :disabled="form.staffpayrollId!=null" placeholder="输入工号和工资年月后自动查询显示"/>
        </el-form-item>
        <el-row>
          <el-col :span="10" style="margin:5px 20px;padding:5px">
            <el-tag type="success">收入项</el-tag>
            <el-form-item label="岗位工资" prop="positionSalary">
              <el-input-number v-model="form.positionSalary" @blur="incomeItemChange()" @change="incomeItemChange()"
                               :precision="2"
                               controls-position="right" :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="薪级工资" prop="payWages">
              <el-input-number v-model="form.payWages" :precision="2" controls-position="right" :min="0" :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="基础性绩效工资" prop="basicPerformancePay">
              <el-input-number v-model="form.basicPerformancePay" @blur="incomeItemChange()"
                               @change="incomeItemChange()" :precision="2"
                               controls-position="right" :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="奖励性绩效工资" prop="incentivizedPerformancePay">
              <el-input-number v-model="form.incentivizedPerformancePay" @blur="incomeItemChange()"
                               @change="incomeItemChange()" :precision="2"
                               controls-position="right"
                               :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="基础性绩效奖" prop="basicPerformanceAward">
              <el-input-number v-model="form.basicPerformanceAward" @blur="incomeItemChange()"
                               @change="incomeItemChange()" :precision="2"
                               controls-position="right" :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="独生子女保健" prop="oneChildCare">
              <el-input-number v-model="form.oneChildCare" @blur="incomeItemChange()" @change="incomeItemChange()"
                               :precision="2"
                               controls-position="right" :min="0" :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="住房补贴" prop="housingSubsidies">
              <el-input-number v-model="form.housingSubsidies" @blur="incomeItemChange()" @change="incomeItemChange()"
                               :precision="2"
                               controls-position="right" :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="加班工资" prop="overtimePay">
              <el-input-number v-model="form.overtimePay" @blur="incomeItemChange()" @change="incomeItemChange()"
                               :precision="2"
                               controls-position="right" :min="0" :step="0.1"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="应发工资" prop="wagesPayable">
              <el-input-number :disabled="true" v-model="form.wagesPayable" :precision="2" controls-position="right"
                               :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
          </el-col>

          <el-col :gutter="2" :span="10" style="margin:5px;padding:5px">
            <el-tag type="danger">扣除项</el-tag>
            <el-form-item
              v-for="(domain, index) in form.floatingItems"
              :label="domain.deductionsType"
              :prop="'floatingItems.' + index + '.cost'"
              :key="domain.id"
            >
              <el-row :gutter="4">
                <el-col :span="5">
                  <el-input-number :precision="2" v-model="domain.cost"
                                   controls-position="right"
                                   :min="0"
                                   :step="0.1"
                                   @change="floatingItemChange()"
                  ></el-input-number>
                  <el-tag style="float:right;" v-if="domain.isRate=='Y'" size="mini" type="primary">百分比项目</el-tag>
                  <!--                  <el-input-number v-if="domain.isRate=='Y'" :precision="4" v-model="domain.rate"
                                                     controls-position="right"
                                                     :min="0"
                                                     :step="0.1"
                                    ></el-input-number>-->
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item label="实扣数" prop="actualDeduction">
              <el-input-number :disabled="true" v-model="form.actualDeduction" :precision="2" controls-position="right"
                               :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-divider></el-divider>
          <el-col :offset="5">
            <el-form-item label="实发数" prop="actualSalary">
              <el-input-number :disabled="true" v-model="form.actualSalary" :precision="2" controls-position="right"
                               :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--历史记录弹出框-->
    <el-dialog :title="title" :visible.sync="openLog" width="850px">

      <div v-show="historyList.length!=0">
        <el-card class="box-card" v-for="(domain, index) in historyList">
          <div slot="header" class="clearfix">
            <span>操作记录{{ index + 1 }}</span>
          </div>
          <div v-for="(item, i) in domain.logInfoList">
            {{ domain.logInfoList[i] }}
          </div>
        </el-card>
      </div>

      <div v-show="historyList.length==0">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>操作记录</span>
          </div>
          <div>
            当前工资表无操作记录
          </div>
        </el-card>
      </div>

    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">

          <el-link type="primary" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">
          1.下载Excel模板<br/>
          2.设置【在编人员工号】整列的单元格格式为【文本】<br/>
          3.其中【在编人员工号】、【姓名】、【员工类型】、【工资年月】、【岗位名称】、【薪级名称】为必填项，其余工资数据如果为空，请填0
        </div>
        <div class="el-upload__tip" style="color:blue" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入结果对话框 -->
    <el-dialog :visible.sync="upload.result" width="400px">
      <div v-html="upload.resultMesg">
        {{ upload.resultMesg }}
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="upload.result = false">确 定</el-button>
        <el-button @click="upload.result = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>

</template>
<script>
import {
  listStaffPayroll,
  getStaffPayroll,
  delStaffPayroll,
  addStaffPayroll,
  updateStaffPayroll,
  getPayrollInformationByJobNumber, determiningStaffPayroll, importTemplate, getFloatingItems
} from '@/api/compensationManagement/staffPayroll'
import { treeselect } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { listStaffPayrollLog } from '@/api/compensationManagement/staffPayrollLog'
import { getToken } from '@/utils/auth'
import { getApproverNameByLike } from '@/api/out/workOvertime'
import { debounce } from 'lodash'
import log from '@/views/monitor/job/log'
// 导入模板接口importTemplate

export default {
  name: 'StaffPayroll',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工资表表格数据
      staffPayrollList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 历史记录
      openLog: false,
      // 历史记录标题
      logTitle: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobnumber: undefined,
        nickName: undefined,
        userEmployeeStatus: undefined,
        userPoliticalOutlook: undefined,
        userDeptId: undefined,
        searchTime: undefined
      },
      // 表单参数
      form: {
        floatingItems: []
      },
      // 表单校验
      rules: {
        jobnumber: [{ required: true, trigger: 'blur', message: '工号不能为空' }],
        salaryyearmonth: [{ required: true, trigger: 'change', message: '工资年月不能为空' }],
        positionSalary: [
          { required: true, trigger: 'blur', message: '岗位工资不能为空' }
        ],
        payWages: [
          { required: true, trigger: 'blur', message: '薪级工资不能为空' }
        ],
        basicPerformancePay: [
          { required: true, trigger: 'blur', message: '基础性绩效工资不能为空' }
        ],
        incentivizedPerformancePay: [
          { required: true, trigger: 'blur', message: '奖励性绩效工资不能为空' }
        ],
        basicPerformanceAward: [
          { required: true, trigger: 'blur', message: '基础性绩效奖不能为空' }
        ],
        oneChildCare: [
          { required: true, trigger: 'blur', message: '独生子女保健不能为空' }
        ],
        housingSubsidies: [
          { required: true, trigger: 'blur', message: '住房补贴不能为空' }
        ],
        overtimePay: [
          { required: true, trigger: 'blur', message: '加班工资不能为空' }
        ],
        nickName: [
          { required: true, trigger: 'blur', message: '姓名不能为空' }
        ],
        position: [
          { required: true, trigger: 'blur', message: '岗位名称不能为空' }
        ],
        salary: [
          { required: true, trigger: 'blur', message: '薪级名称不能为空' }
        ]
      },
      //员工状态选项
      employeeStatusOptions: [
        {
          id: 1,
          value: '试用',
          label: '试用'
        },
        {
          id: 2,
          value: '在职',
          label: '在职'
        },
        {

          id: 3,
          value: '退休',
          label: '退休'
        },
        {
          id: 4,
          value: '离职',
          label: '离职'
        }
      ],
      //政治面貌选项
      mianmaoOptions: [
        {
          id: 1,
          value: '党员',
          label: '党员'
        },
        {
          id: 2,
          value: '团员',
          label: '团员'
        },
        {
          id: 3,
          value: '群众',
          label: '群众'
        },
        {
          id: 4,
          value: '其他',
          label: '其他'
        }

      ],
      //历史记录列表
      historyList: [],
      //strList
      strList: null,
      salaryQueryDate: {
        jobnumber: undefined,
        date: undefined
      },

      /* 工资表导入 */
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 是否显示弹出层（导入结果）
        result: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/compensationManagement/staffPayroll/importData',
        //上传结果
        resultMesg: null
      },

      //审核状态
      auditStatusOptions: [
        {
          id: 1,
          value: 'Y',
          label: '确定'
        },
        {
          id: 2,
          value: 'N',
          label: '未确定'
        }
      ],

      userObj: {
        approverOptions: [],
        loading: false
      },
      approverOptions: null
    }

  },
  created() {
    this.getList()
    this.getTreeselect()
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data
      })
    },
    /** 查询工资表列表 */
    getList() {
      this.loading = true
      listStaffPayroll(this.queryParams).then(response => {
        this.staffPayrollList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        staffpayrollId: null,
        jobnumber: null,
        nickName: null,
        employeeType: null,
        salaryyearmonth: null,
        position: null,
        salary: null,
        positionSalary: null,
        payWages: null,
        basicPerformancePay: null,
        incentivizedPerformancePay: null,
        basicPerformanceAward: null,
        oneChildCare: null,
        housingSubsidies: null,
        overtimePay: null,
        wagesPayable: null,
        deductionsPart: null,
        actualDeduction: null,
        actualSalary: null,
        auditStatus: '0',
        floatingItems: null,
        selectUser: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.staffpayrollId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      getFloatingItems().then(response => {
        this.form.floatingItems = response.data
      })
      this.open = true
      this.title = '添加工资表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const staffpayrollId = row.staffpayrollId || this.ids
      getStaffPayroll(staffpayrollId).then(response => {
        this.form = response.data
        this.form.floatingItems = response.data.deductionsPartSql
        this.open = true
        this.title = '修改工资表'
      })
    },
    /** 历史记录按钮操作 */
    viewModificationHistory(row) {
      const CompensationStaffPayrollLog = {
        staffpayrollId: row.staffpayrollId
      }
      listStaffPayrollLog(CompensationStaffPayrollLog).then(response => {
        this.historyList = response.rows
        this.openLog = true
        this.logTitle = '历史修改记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.staffpayrollId != null) {
            updateStaffPayroll(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            this.form.deductionsPartSql = this.form.floatingItems
            addStaffPayroll(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const staffpayrollIds = row.staffpayrollId || this.ids
      this.$modal.confirm('是否确认删除工资表编号为"' + staffpayrollIds + '"的数据项？').then(function() {
        return delStaffPayroll(staffpayrollIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出工资条按钮操作 */
    handleExportArticleWages() {
      this.download(`compensationManagement/staffPayroll/exportArticleWages`, {
        ...this.queryParams
      }, `工资条_${new Date().getTime()}.xlsx`)
    },
    /**导出工资档案 */
    handleExportSalaryFiles() {
      this.download(`compensationManagement/staffPayroll/exportSalaryFiles`, {
        ...this.queryParams
      }, `工资档案_${new Date().getTime()}.xlsx`)
    },
    //工号输入框搜索
    jobnumberChange() {
      if (this.form.jobnumber != null && this.form.salaryyearmonth != null && this.form.staffpayrollId === null) {
        this.salaryQueryDate.jobnumber = this.form.jobnumber
        this.salaryQueryDate.date = this.form.salaryyearmonth
        this.salaryQueryDate.selectUser = this.form.selectUser
        this.reset()
        this.form.jobnumber = this.salaryQueryDate.jobnumber
        this.form.salaryyearmonth = this.salaryQueryDate.date
        this.form.selectUser = this.salaryQueryDate.selectUser
        getPayrollInformationByJobNumber(this.salaryQueryDate).then(response => {
          //姓名
          this.form.nickName = response.data.nickName
          //员工类型
          this.form.employeeType = response.data.employeeType
          //岗位名称
          this.form.position = response.data.position

          //薪级名称
          if (response.data.positionSalaryInfo != null) {
            //薪级名称
            this.form.salary = response.data.positionSalaryInfo.positionName
            //岗位工资
            this.form.positionSalary = response.data.positionSalaryInfo.positionSalary
            //基础性绩效工资
            this.form.basicPerformancePay = response.data.positionSalaryInfo.basicPerformancePay

            //奖励性绩效工资
            this.form.incentivizedPerformancePay = response.data.positionSalaryInfo.incentivizedPerformancePay

            //基础性绩效奖
            this.form.basicPerformanceAward = response.data.positionSalaryInfo.basicPerformanceAward
          }
          //薪级工资
          this.form.payWages = response.data.positionSalary

          //加班工资
          this.form.overtimePay = response.data.overtimePay

          //浮动项目
          this.form.floatingItems = response.data.listDeductionAmountSettings

          this.form.wagesPayable = (this.form.positionSalary * 100 + this.form.payWages * 100 + this.form.basicPerformancePay * 100 + this.form.incentivizedPerformancePay * 100 + this.form.basicPerformanceAward * 100 + this.form.oneChildCare * 100 + this.form.housingSubsidies * 100 + this.form.overtimePay * 100) / 100

          if (this.form.floatingItems != null) {
            var sum = 0
            for (var i in this.form.floatingItems) {
              sum = sum + this.form.floatingItems[i].cost * 100
            }
            this.form.actualDeduction = sum / 100
            //实发数
            this.form.actualSalary = (this.form.wagesPayable * 100 - this.form.actualDeduction * 100) / 100
          }
        })
      }

    },

    //重新计算应发工资、实发数
    incomeItemChange() {
      this.form.wagesPayable = (this.form.positionSalary * 100 + this.form.payWages * 100 + this.form.basicPerformancePay * 100 + this.form.incentivizedPerformancePay * 100 + this.form.basicPerformanceAward * 100 + this.form.oneChildCare * 100 + this.form.housingSubsidies * 100 + this.form.overtimePay * 100) / 100
      //实发数
      this.form.actualSalary = (this.form.wagesPayable * 100 - this.form.actualDeduction * 100) / 100
    },
    //重新计算实扣数、实发数
    floatingItemChange() {
      if (this.form.floatingItems != null) {
        var sum = 0
        for (var i in this.form.floatingItems) {
          sum = sum + this.form.floatingItems[i].cost * 100
        }
        this.form.actualDeduction = sum / 100
        //实发数
        this.form.actualSalary = (this.form.wagesPayable * 100 - this.form.actualDeduction * 100) / 100
      }
    },

    //确定工资表
    determinePayroll(row) {
      this.$modal.confirm('是否确定工号为:' + row.jobnumber + '，姓名为：' + row.nickName + '的补贴工资表？').then(function() {
        return determiningStaffPayroll(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('确定成功')
      }).catch(() => {
      })
    },

    /* 工资导入 */
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '历史工资导入'
      this.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('compensationManagement/staffPayroll/importTemplate', {}, `工资导入模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.upload.result = true
      this.upload.resultMesg = response.msg
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },

    remoteMethod: debounce(function(query) {
      if (query !== '') {
        this.userObj.loading = true
        //发送请求，根据用户姓名进行模糊查询
        getApproverNameByLike(query).then((res) => {
          this.userObj.loading = false
          this.userObj.approverOptions = res.data.map((item) => {
            return {
              value: `${item.jobNumber}`,
              label: `账号: ${item.userName}   姓名: ${item.nickName}  部门: ${item.deptName}`
            }
          })
        })
      } else {
        this.approverOptions = []
      }
    }, 500),

    userChange() {
      this.form.jobnumber = this.form.selectUser
      this.jobnumberChange()
    }
  }
}
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
}
</style>
